package cn.xerllent.wxtp.service.mapper;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import cn.xerllent.config.MybatisRedisCache;
import cn.xerllent.wxtp.entity.Dksb;
import cn.xerllent.wxtp.entity.DksbCourse;
import cn.xerllent.wxtp.service.dto.DksbFilter;


@Repository
@Mapper
@CacheNamespace(implementation = MybatisRedisCache.class, eviction = MybatisRedisCache.class)
public interface DksbCourseMapper extends BaseMapper<DksbCourse> {
    /**
     * 查询参数包含DksbCourse，需要联表查询
     * */
    @Options(useCache = false)
    @Select("<script>"
    		+ "SELECT c.* FROM `wxtp_dksb` d left outer join `wxtp_dksb_course` c on d.id=c.dksb_id "
			+ "<where>"
			+ " d.corp_id = #{corpId} AND d.status &lt; 3 "
    		+ "<if test = \"course.dksbId!=null and course.dksbId!=''\" > AND c.dksb_id &lt;&gt; #{course.dksbId} </if>"
    		+ "<if test = \"course.eventdate!=null\" > AND c.eventdate = #{course.eventdate} </if>"
    		+ "<if test = \"course.grade!=null and course.grade!=''\" > AND c.grade = #{course.grade} </if>"
    		+ "<if test = \"course.clasz!=null and course.clasz!=''\" > AND c.clasz = #{course.clasz} </if>"
    		+ "<if test = \"course.coursetime!=null and course.coursetime!=''\" > AND c.coursetime = #{course.coursetime} </if>"
    		+ "</where>"
    		+ " limit 1"
    		+ "</script>")
    DksbCourse checkRepeat(@Param("corpId") String corpId,@Param("course") DksbCourse course);
    
    
    @Select("SELECT * FROM `wxtp_dksb_course` where dksb_id=#{dksbId}")
    List<DksbCourse> getListByDksbId(@Param("dksbId") Long dksbId);
    
    /**
     * 联表查询course结果
     * */
    @Options(useCache = false)
    @Select("<script>"
    		+ "SELECT c.* FROM `wxtp_dksb` d inner join `wxtp_dksb_course` c on d.id=c.dksb_id"
			+ "<where>"
			+ "<if test = \"corpId!=null\" > AND d.corp_id = #{corpId} </if>"
    		+ "<if test = \"blurry!=null and blurry!=''\" >"
    		+ " AND ("
	    		+ "d.comment LIKE concat('%', #{blurry}, '%') "
    		+ ")"
    		+ "</if>"
    		+ "<if test = \"status!=null and status!=''\" > AND d.status = #{status} </if>"
    		+ "<if test = \"reqUserid!=null and reqUserid!=''\" > AND d.req_userid = #{reqUserid} </if>"
    		+ "<if test = \"srcUserid!=null and srcUserid!=''\" > AND c.src_userid = #{srcUserid} </if>"
    		+ "<if test = \"tarUserid!=null and tarUserid!=''\" > AND c.tar_userid = #{tarUserid} </if>"
    		+ "<if test = \"grade!=null and grade!=''\" > AND c.grade LIKE concat(#{grade}, '%') </if>"
    		+ "<if test = \"clasz!=null and clasz!=''\" > AND c.clasz = #{clasz} </if>"
    		+ "<if test = \"coursetime!=null and coursetime!=''\" > AND c.coursetime = #{coursetime} </if>"
    		+ "<if test = \"subject!=null and subject!=''\" > AND c.subject = #{subject} </if>"
    		
//    		+ "<if test = \"createtime !=null and createtime.size()>1\" > "
//    			+ " AND d.createtime BETWEEN  #{createtime[0]} and #{createtime[1]}"
//    		+ "</if>"
//    		+ "<if test = \"eventdate !=null and eventdate.size()>1\" > "
//				+ " AND c.eventdate BETWEEN  #{eventdate[0]} and #{eventdate[1]}"
//			+ "</if>"
				
    		+ "<if test = \"createtimeFrom !=null\" > "
				+ " AND d.createtime &gt;= #{createtimeFrom}"
			+ "</if>"
			+ "<if test = \"createtimeTo !=null\" > "
				+ " AND d.createtime &lt; #{createtimeTo}"
			+ "</if>"
	
			+ "<if test = \"eventdateFrom !=null\" > "
				+ " AND c.eventdate &gt;= #{eventdateFrom}"
			+ "</if>"
			+ "<if test = \"eventdateTo !=null\" > "
				+ " AND c.eventdate &lt; #{eventdateTo}"
			+ "</if>"
    		
    		+ "</where>"
    		+ " order by c.eventdate desc, c.coursetime desc"
    		+ "</script>")
    List<DksbCourse> queryWithDksb(DksbFilter filter);
}